package com.library.controller;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.text.html.parser.Entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.library.entity.Lesson;
import com.library.service.ILessonService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 至尊伍人组
 * @since 2018-09-30
 */
@Controller
@SessionAttributes(value= {"lessonTypes","admin","reader"})
@RequestMapping("/lesson")
public class LessonController {
	@Autowired
	ILessonService lessonServiceImpl;
	 
	
	/**课程管理的初始页面
	 * @param model
	 * @param lesson
	 * @return
	 */
	@RequestMapping("/admin/selectLesson")
	public String selectLesson(ModelMap model) {
		List<Lesson> lessons=lessonServiceImpl.selectList(new EntityWrapper<Lesson>());
		Set<String> lessonTypes=new HashSet<String>();
		for(Lesson lesson:lessons) {
			lessonTypes.add(lesson.getLessonType());
		}
		List<Lesson> lessonList=lessonServiceImpl.selectList(new EntityWrapper<Lesson>());
		model.addAttribute("lessonList", lessonList);
		model.addAttribute("lessonTypes",lessonTypes );
		return "admin/adminLessons";
	}
	
	/**搜索按钮跳转页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/admin/selectLessonInfo")
	public String selectLesson(ModelMap model,Lesson selectLesson) {
		//创建条件构造器
		System.out.println(selectLesson);
		Wrapper<Lesson> wrapper=new EntityWrapper<Lesson>();
		if(selectLesson.getLessonId()==null) {
			if(selectLesson.getLessonName()!=null)
				wrapper=wrapper.like("lessonName", selectLesson.getLessonName());
			wrapper=wrapper.eq("lessonType", selectLesson.getLessonType());
		}else {
			wrapper=wrapper.eq("lessonId", selectLesson.getLessonId());
		}
		List<Lesson> lessonList=lessonServiceImpl.selectList(wrapper);
		model.addAttribute("lessonList", lessonList);
		return "admin/adminLessons";
	}
	
	/**选择考试课程
	 * @param model
	 * @return
	 */
	@RequestMapping("/reader/chooseTest")
	public String chooseTest(ModelMap model) {
		List<Lesson> lessons=lessonServiceImpl.selectList(new EntityWrapper<Lesson>());
		model.addAttribute("lessons",lessons );
		return "reader/chooseTest";
	}
	
	/**管理员添加课程
	 * @param model
	 * @param lesson
	 * @return
	 */
	@RequestMapping("add")
	public String addLesson(ModelMap model,Lesson lesson) {
		System.out.println(lesson);
		lessonServiceImpl.insert(lesson);
		return "redirect:/lesson/admin/selectLesson";
	}
	
	/**管理员更新课程
	 * @param model
	 * @param lesson
	 * @return
	 */
	@RequestMapping("update")
	public String updaetLesson(ModelMap model, Lesson lesson) {
		System.out.println(lesson);
		lessonServiceImpl.updateById(lesson);
		return "redirect:/lesson/admin/selectLesson";
	}
	/**管理员删除课程
	 * @param model
	 * @param lessonId
	 * @return
	 */
	@RequestMapping("del/{lessonId}")
	public String deleteLesson(ModelMap model,@PathVariable int lessonId){
//		System.out.println(lessonId);
		lessonServiceImpl.delete(new EntityWrapper<Lesson>().eq("lessonId", lessonId));
		return "redirect:/lesson/admin/selectLesson";
	}
}

